package com.atguigu.Controller.consumer;

import com.atguigu.constant.MqConst;
import com.atguigu.service.SearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.naming.Binding;
import javax.script.Bindings;
import java.io.IOException;

@Component
public class EsConsumer {
    @Autowired
    private SearchService searchService;
    @RabbitListener(bindings=@QueueBinding(value = @Queue(value = MqConst.ON_SALE_QUEUE,durable = "false"),
    exchange  =@Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key  ={MqConst.ON_SALE_ROUTING_KEY}))
    public void onSale(Long skuId, Channel channel, Message message) throws IOException {
        if (skuId!=null){
            searchService.onSale(skuId);
        }
        //默认发布确认方式,需要手动签收
        //multipe是否应答多个消息.TRUE签收多个消息,false之签收当前消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



    @RabbitListener(bindings=@QueueBinding(value = @Queue(value = MqConst.OFF_SALE_QUEUE,durable = "false"),
            exchange  =@Exchange(value = MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key  ={MqConst.OFF_SALE_ROUTING_KEY}))
    public void offSale(Long skuId, Channel channel, Message message) throws IOException {
        if (skuId!=null){
            searchService.offSale(skuId);
        }
        //默认发布确认方式,需要手动签收
        //multipe是否应答多个消息.TRUE签收多个消息,false之签收当前消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
